package com.icss.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;


import com.icss.util.Constants;
import com.icss.helper.RequestHelper;
import com.icss.helper.StringHelper;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport
		implements
			SessionAware,
			ServletRequestAware,
			ApplicationAware,
			ServletResponseAware {
	public Map session;

	public HttpServletRequest request;

	public HttpServletResponse response;

	public Map application;

	public StringHelper str = new StringHelper();

	public RequestHelper req = new RequestHelper();


	// ��־��¼��
	public final Logger log = Logger.getLogger(this.getClass());

	/**
	 * 
	 */
	public BaseAction() {
		// TODO Auto-generated constructor stub
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public void setApplication(Map application) {
		this.application = application;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	// һЩ�������������޹أ�
	public String getValue(String paramName) {
		return req.getValue(request, paramName);
	}

	public String[] getValues(String paramName) {
		return req.getValues(request, paramName);
	}

	public String getAjaxValue(String paramName) {
		return req.getAjaxValue(request, paramName);
	}

	public String[] getAjaxValues(String paramName) {
		return req.getAjaxValues(request, paramName);
	}

	// ��ӡ��ǰrequest���в���
	public void printReqParameters() {
		Enumeration paramNames = this.request.getParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();

			System.out.println("String " + paramName
					+ " = req.getValue(request,\"" + paramName + "\");");
		}
	}


	public void writeText(Object data) {
		try {
			response.getWriter().print(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	public void writeExcel(HSSFWorkbook wb) throws IOException {
		response.reset();
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=result.xls");
		wb.write(response.getOutputStream());
		response.getOutputStream().flush();
		response.getOutputStream().close();
	}
	
	public void writeExcel(HSSFWorkbook wb, String fileName) throws IOException {
		fileName = (fileName == null || fileName.equals("")) ? "result" : fileName;
		fileName = java.net.URLEncoder.encode(fileName, "UTF-8").replace('+', ' ');
		response.reset();
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
		wb.write(response.getOutputStream());
		response.getOutputStream().flush();
		response.getOutputStream().close();
	}
	
	/**
	 * �ļ�����
	 * 
	 * @param file_path
	 *            �ļ�����·�� �磺/upload/teamwork/�Ӻ�·��¥��ϯ����20100826110607.xls
	 * @param fileName
	 *            �ļ���� �磺�Ӻ�·��¥��ϯ����
	 * @throws IOException
	 */
	public void downloadFile(String file_path, String fileName)
			throws IOException {
		file_path = "";
		System.out.println(file_path);
		File downfile = new File(file_path);

		fileName = java.net.URLEncoder.encode(fileName, "UTF-8").replace('+',
				' ');
		ServletOutputStream out1 = response.getOutputStream();

		if (!downfile.exists()) {
			response.setContentType("application/x-download");
			out1.print("file doesn't exist");
			return;
		} else {
			// ����ļ�ͷ
			response.setContentType("text/xml;charset=UTF-8");
			response.setContentType("application/x-download");
			response.setHeader("Content-Disposition", "attachment; filename=\""
					+ fileName + "\"");

			// ȷ��IEʶ�𱾴�Ϊ�����ļ�
			response.setHeader("Content-Transfer-Encoding", "binary");
			response.setHeader("Cache-Control",
					"must-revalidate, post-check=0, pre-check=0");
			response.setHeader("Pragma", "public");
		}

		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(downfile));
			bos = new BufferedOutputStream(out1);
			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (IOException e) {
			throw e;
		} finally {
			if (bis != null)
				bis.close();
			if (bos != null)
				bos.close();
		}
	}
	
	public void download() {
		String fileName = getValue("fileName");
		String file_path = getValue("fileUrl");
		if ("".equals(fileName))
			fileName = file_path.substring(file_path.lastIndexOf("/") + 1,
					file_path.length());
		try {
			downloadFile(file_path, fileName);
		} catch (IOException e) {
			this.log.debug("�����ļ������쳣��");
		}
	}
	
}
